package com.wy.System.WindowCommand;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2024/03/16/23:27
 * @target 心若有所向往，何惧道阻且长
 */

//想在这儿测试一下端是否 能用
public class Unit {
    private static Runtime runtime = Runtime.getRuntime();

    public static void endClose(Process process) {
        try {
            process.waitFor();
            process.destroy();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static void executeCommand(String commandName) {
        try {
            Process process = runtime.exec(commandName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            endClose(process);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //    判断端口是否被占用，被占用返回占用的pid 和 进程名字  没有被占用返回可用
//    判断端口
    public static void JudgmentPortAvailabilityOrNot(String port) {
        try {
            Process process = Runtime.getRuntime().exec("netstat -ano");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains(":" + port + " ")) {
//                    ，split("\\s+")会根据一个或多个空白字符将字符串分割成多个部分，并返回一个包含这些部分的字符串数组。
                    String[] parts = line.trim().split("\\s+");
                    String pid = parts[parts.length - 1];
                    String command = "tasklist /FI \"PID eq " + pid + "\"";
                    Process tasklistProcess = Runtime.getRuntime().exec(command);
                    BufferedReader tasklistReader = new BufferedReader(new InputStreamReader(tasklistProcess.getInputStream(), Charset.forName("GBK")));
                    String tasklistLine;
                    int Line = 0;
                    System.out.println("端口 " + port + " 已被占用");
                    while ((tasklistLine = tasklistReader.readLine()) != null) {
                        ++Line;
                        if (Line == 4 || Line == 2) {
                            System.out.println(tasklistLine);
                        }
                    }
                    return;
                }
            }
            System.out.println("端口 " + port + " 可用");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //    杀死某一个端口进程
    public static void killProcessByPort(String pid) throws IOException {
        Process exec = runtime.exec("taskkill /pid  " + pid + "  /F");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(exec.getInputStream(), Charset.forName("GBK")));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        endClose(exec);
    }

    //    根据端口查询pid
    public static String  selectPidByPort(String port) {
        return new String("pid");
    }

    public static void main(String[] args) {
//        netstat -ano | findstr :8080  tasklist | findstr <PID>
        JudgmentPortAvailabilityOrNot("8080");
    }

    @Test
    public void test1() {
        try {
            killProcessByPort("23712");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


//重复的东西有些多，  不知道该怎么改。